package sliding_window;

import java.util.Hashtable;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-12-04
 * Time: 12:42
 */
public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        char[] chars = s.toCharArray();
        int[] hash = new int[128];//用数组模拟哈希表, 用值表示字符出现的次数.
        int ans = 0;
        int right = 0, left = 0;
        while (right < s.length()) {
            hash[chars[right]]++;//进窗口
            while (hash[chars[right]] > 1) {//判断, 只要有重复字符, 就让left指向的字符划出窗口
                hash[chars[left++]]--;//出窗口
            }
            ans = Math.max(ans, right - left + 1);//更新结果
            right++;
        }
        return ans;
    }
}
